package com.ehais.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.ehais.modbuswifi.MainStationActivity;
import com.ehais.modbuswifi.MyApplication;
import com.ehais.modbuswifi.R;
import com.ehais.model.AiStation;

import java.util.List;

public class StationRecyclerAdapter extends RecyclerView.Adapter<StationRecyclerAdapter.ViewHolder>{

    private List<AiStation.AiStationModel> stationData;//数据源
    private Context myContext;
    private OnItemClickListener myClickListener;
    public  View mview;
    public StationRecyclerAdapter(List<AiStation.AiStationModel> stationData) {
        this.stationData = stationData;
        Log.i("StationRecyclerAdapter","this.stationData:"+this.stationData.size());
    }
    //静态内部类， 每个条目对应的布局
    static class ViewHolder extends RecyclerView.ViewHolder{
        TextView tvStationName;
        TextView tvStationCode;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvStationName = (TextView) itemView.findViewById(R.id.tvStationName);
            tvStationCode = (TextView) itemView.findViewById(R.id.tvStationCode);


        }
    }
    //用于创建ViewHolder实例,并把加载的布局传入到ViewHolder的构造函数去
    @NonNull
    @Override
    public StationRecyclerAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_station_item, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }
    //是用于对子项的数据进行赋值,会在每个子项被滚动到屏幕内时执行。position得到当前项的实例
    @Override
    public void onBindViewHolder(@NonNull StationRecyclerAdapter.ViewHolder holder, int position) {
        AiStation.AiStationModel stationModel = stationData.get(position);
        holder.tvStationName.setText(stationModel.getStationName());
        holder.tvStationCode.setText(stationModel.getStationCode());

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("ResourceAsColor")
            @Override
            public void onClick(View v) {
//                Toast.makeText(MyApplication.getInstance(), "" + holder.names.getText()+"  "+position, Toast.LENGTH_SHORT).show();
                myClickListener.OnClick(position);
            }
        });
    }
    //返回RecyclerView的子项数目
    @Override
    public int getItemCount() {
        return this.stationData.size();
    }


    public void setOnItemClickListener(OnItemClickListener myClickListener) {
        this.myClickListener = myClickListener;
    }

    public interface OnItemClickListener{
        void OnClick(int position);
    }


}
